﻿package core.ui
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.ui.*;

    public class LayersPanel extends Box
    {
        private var doc:Document;
        private var cm:ContextMenu;
        private var bbar:Sprite;
        private var grip:Sprite;
        private var holder:Sprite;
        private var scroller:Scrollbar;
        private var opacity:Dropslider;
        private var blendmode:Dropdown;
        private var newlayer:IconButton;
        private var trash:IconButton;
        private var moveup:IconButton;
        private var movedown:IconButton;
        private var layerstyle:IconButton;
        private var layermask:IconButton;
        var l:LayerInfo;
        var md:Point;
        var nr:int;
        var ghost:Bitmap;
        var marker:Sprite;
        var draggin:Boolean;

        public function LayersPanel(param1:Tuyitu)
        {
            this.md = new Point();
            super(param1, "rm_layers", 220, 158);
            var _loc_2:* = new BoxSplit("medium");
            _loc_2.x = 11;
            _loc_2.y = 42;
            addChild(_loc_2);
            this.holder = new Sprite();
            this.holder.addEventListener(MouseEvent.ROLL_OVER, this.HolderMouseOver, false, 0, true);
            this.holder.addEventListener(MouseEvent.ROLL_OUT, this.HolderMouseOut, false, 0, true);
            this.holder.x = 4;
            this.holder.y = 44;
            addChild(this.holder);
            this.scroller = new Scrollbar(this.holder, new Rectangle(0, 0, 212, 87));
            this.scroller.addEventListener(ScrollEvent.APPEARANCE, this.Update, false, 1, true);
            this.scroller.x = 200;
            this.scroller.y = 44;
            addChild(this.scroller);
            this.cm = new ContextMenu();
            this.cm.hideBuiltInItems();
            this.cm.addEventListener(ContextMenuEvent.MENU_SELECT, this.ContextPrerender, false, 1, true);
            UI.AddToContext(this.cm, "rm_duplicate_layer", param1.cm.DuplicateLayer);
            UI.AddToContext(this.cm, "rm_delete_layer", param1.cm.DeleteLayer);
            UI.AddToContext(this.cm, "rm_merge_down", param1.cm.MergeLayerDown, true);
            UI.AddToContext(this.cm, "rm_merge_visible", param1.cm.MergeVisibleLayers);
            UI.AddToContext(this.cm, "rm_flatten_image", param1.cm.FlattenImage);
            UI.AddToContext(this.cm, "rm_rasterize_layer", param1.cm.RasterizeType, true);
            UI.AddToContext(this.cm, "rm_layer_style", param1.cm.ShowLayerStyle, true);
            UI.AddToContext(this.cm, "rm_copy_layer_style", param1.cm.CopyLayerStyle);
            UI.AddToContext(this.cm, "rm_paste_layer_style", param1.cm.PasteLayerStyle);
            UI.AddToContext(this.cm, "rm_clear_layer_style", param1.cm.ClearLayerStyle);
            UI.AddToContext(this.cm, "rm_add_layer_mask", param1.cm.AddLayerMask, true);
            UI.AddToContext(this.cm, "rm_delete_layer_mask", param1.cm.DeleteLayerMask);
            UI.AddToContext(this.cm, "rm_apply_layer_mask", param1.cm.ApplyLayerMask);
            this.holder.contextMenu = this.cm;
            this.grip = new Sprite();
            this.grip.addChild(new Bitmap(new DragMark()));
            this.grip.addEventListener(MouseEvent.MOUSE_DOWN, this.GripMouseDown, false, 0, true);
            this.grip.addEventListener(MouseEvent.MOUSE_OVER, this.GripMouseOver, false, 0, true);
            this.grip.addEventListener(MouseEvent.MOUSE_OUT, this.GripMouseOut, false, 0, true);
            this.grip.x = 203;
            this.grip.y = 140;
            addChild(this.grip);
            var _loc_3:* = new Literal("rm_opacity", true);
            _loc_3.setTextFormat(new TextFormat("Verdana, Helvetica, San-serif", 8, 0, null, null, null, null, null, "right"));
            _loc_3.x = 86;
            _loc_3.y = 21;
            _loc_3.width = 80;
            addChild(_loc_3);
            this.opacity = new Dropslider(48, 0, 100, 100);
            this.opacity.addEventListener(SliderEvent.DRAG, this.OpacityChange, false, 0, true);
            this.opacity.x = 165;
            this.opacity.y = 21;
            this.opacity.Disable();
            addChild(this.opacity);
            this.bbar = new Sprite();
            var _loc_4:* = new BoxSplit("medium");
            _loc_4.x = 11;
            _loc_4.y = 0;
            this.bbar.addChild(_loc_4);
            this.movedown = new IconButton(param1, "rm_move_layer_down", new DownIcon());
            this.movedown.addEventListener(MouseEvent.CLICK, param1.cm.OrderLayerDown, false, 0, true);
            this.movedown.x = 33;
            this.movedown.y = 6;
            this.bbar.addChild(this.movedown);
            this.moveup = new IconButton(param1, "rm_move_layer_up", new UpIcon());
            this.moveup.addEventListener(MouseEvent.CLICK, param1.cm.OrderLayerUp, false, 0, true);
            this.moveup.x = 15;
            this.moveup.y = 5;
            this.bbar.addChild(this.moveup);
            this.layermask = new IconButton(param1, "rm_add_layer_mask", new MaskIcon());
            this.layermask.addEventListener(MouseEvent.CLICK, param1.cm.AddLayerMask, false, 0, true);
            this.layermask.x = 54;
            this.layermask.y = 5;
            this.bbar.addChild(this.layermask);
            this.layerstyle = new IconButton(param1, "rm_layer_style", new FilterIcon());
            this.layerstyle.addEventListener(MouseEvent.CLICK, param1.cm.ShowLayerStyle, false, 0, true);
            this.layerstyle.x = 75;
            this.layerstyle.y = 3;
            this.bbar.addChild(this.layerstyle);
            this.newlayer = new IconButton(param1, "rm_new_layer", new NewIcon());
            this.newlayer.addEventListener(MouseEvent.CLICK, param1.cm.NewEmptyLayer, false, 0, true);
            this.newlayer.x = 155;
            this.newlayer.y = 5;
            this.bbar.addChild(this.newlayer);
            this.trash = new IconButton(param1, "rm_delete_layer", new TrashIcon());
            this.trash.addEventListener(MouseEvent.CLICK, param1.cm.DeleteLayer, false, 0, true);
            this.trash.x = 176;
            this.trash.y = 5;
            this.bbar.addChild(this.trash);
            this.bbar.x = 4;
            this.bbar.y = 132;
            addChild(this.bbar);
            this.blendmode = new Dropdown(90, 90);
            this.blendmode.addEventListener(SelectEvent.COMMAND, this.BlendModeChange, false, 0, true);
            this.blendmode.x = 8;
            this.blendmode.y = 21;
            this.blendmode.AddItem("正常", "normal", true);
            this.blendmode.AddItem("添加", "add");
            this.blendmode.AddItem("透明度", "alpha");
            this.blendmode.AddItem("变暗", "darken");
            this.blendmode.AddItem("差值", "difference");
            this.blendmode.AddItem("排除", "erase");
            this.blendmode.AddItem("强光", "hardlight");
            this.blendmode.AddItem("倒置", "invert");
            this.blendmode.AddItem("层叠", "layer");
            this.blendmode.AddItem("变亮", "lighten");
            this.blendmode.AddItem("正片叠底", "multiply");
            this.blendmode.AddItem("叠加", "overlay");
            this.blendmode.AddItem("屏幕", "screen");
            this.blendmode.AddItem("减淡", "subtract");
            this.blendmode.Bind();
            this.blendmode.Disable();
            addChild(this.blendmode);
            return;
        }// end function

        private function NewLayer(... args) : void
        {
            ed.cm.NewLayer();
            return;
        }// end function

        public function SetReference(rm_opacity:Document) : void
        {
            this.doc = rm_opacity;
            this.Update();
            return;
        }// end function

        public function Update(... args) : void
        {
            args = null;
            var _loc_3:int = 0;
            this.RemoveMarkers();
            while (this.holder.numChildren > 0)
            {
                
                this.holder.removeChildAt(0);
            }
            if (this.doc != null)
            {
                _loc_3 = 0;
                while (_loc_3 < this.doc.numChildren)
                {
                    
                    args = new LayerInfo(ed, _loc_3, this.doc.getChildAt(_loc_3) as Layer, this.doc.dw, this.doc.dh, this.scroller.visible ? (196) : (212));
                    args.addEventListener(MouseEvent.MOUSE_DOWN, this.LayerMouseDown, false, 0, true);
                    args.y = int(39 * ((this.doc.numChildren - 1) - _loc_3));
                    this.holder.addChild(args);
                    if (_loc_3 == this.doc.anr)
                    {
                        this.MarkLayer(args, false);
                    }
                    _loc_3++;
                }
                if (ed.canvas != null)
                {
                    ed.canvas.UpdateAfterEdit();
                }
            }
            this.scroller.SetContentHeight(this.holder.numChildren * 39);
            return;
        }// end function

        public function UpdateLayerBitmap(rm_opacity:int = -1) : void
        {
            if (this.doc != null)
            {
                if (rm_opacity == -1)
                {
                    rm_opacity = this.doc.anr;
                }
                if (rm_opacity < this.holder.numChildren)
                {
                    (this.holder.getChildAt(rm_opacity) as LayerInfo).UpdateBitmap();
                }
            }
            return;
        }// end function

        private function ContextPrerender(event:ContextMenuEvent) : void
        {
            var _loc_2:LayerInfo = null;
            var _loc_3:int = 0;
            while (_loc_3 < this.holder.numChildren)
            {
                
                _loc_2 = this.holder.getChildAt(_loc_3) as LayerInfo;
                if (_loc_2.hitTestPoint(stage.mouseX, stage.mouseY))
                {
                    break;
                }
                _loc_3++;
            }
            if (_loc_2.nr != ed.canvas.document.anr)
            {
                this.MarkLayer(_loc_2, true);
            }
            this.cm.customItems[1].enabled = Boolean(ed.cm.CheckNotLockedLayer() && ed.canvas.document.numChildren > 1);
            this.cm.customItems[2].enabled = Boolean(_loc_2.nr != 0);
            this.cm.customItems[3].enabled = Boolean(ed.canvas.document.numChildren > 1);
            this.cm.customItems[4].enabled = Boolean(ed.canvas.document.numChildren > 1);
            this.cm.customItems[5].enabled = Boolean(_loc_2.layer.type != LayerType.Bitmap);
            this.cm.customItems[7].enabled = Boolean(_loc_2.layer.filters != null && _loc_2.layer.filters.length > 0);
            this.cm.customItems[8].enabled = Boolean(ed.clipboard.layerstyle != null);
            this.cm.customItems[9].enabled = Boolean(_loc_2.layer.filters != null && _loc_2.layer.filters.length > 0);
            this.cm.customItems[10].enabled = Boolean(_loc_2.layer.type == LayerType.Bitmap && !_loc_2.layer.hasMask && ed.cm.CheckNotLockedLayer());
            this.cm.customItems[11].enabled = _loc_2.layer.hasMask;
            this.cm.customItems[12].enabled = _loc_2.layer.hasMask;
            return;
        }// end function

        private function MarkLayer(param1:LayerInfo, param2:Boolean)
        {
            if (this.doc.anr < this.holder.numChildren)
            {
                (this.holder.getChildAt(this.doc.anr) as LayerInfo).Marking(false);
            }
            param1.Marking(true);
            this.doc.anr = param1.nr;
            var _loc_3:* = this.doc.GetLayer();
            this.opacity.Set(Math.round(_loc_3.alpha * 100));
            this.opacity.Disable(param1.nr == 0 && !this.doc.transparent);
            this.blendmode.Set(_loc_3.blendMode);
            this.blendmode.Disable(param1.nr == 0 && !this.doc.transparent);
            if (param2)
            {
                ed.tool.Reset();
            }
            return;
        }// end function

        private function OpacityChange(... args) : void
        {
            ed.canvas.layer.alpha = this.opacity.value / 100;
            return;
        }// end function

        private function BlendModeChange(event:SelectEvent) : void
        {
            ed.canvas.layer.blendMode = event.val;
            ed.canvas.layer.alpha = ed.canvas.layer.alpha;
            ed.canvas.UpdateAfterEdit();
            ed.canvas.UpdateHolderSize();
            return;
        }// end function

        private function LayerMouseDown(event:MouseEvent) : void
        {
            this.l = event.currentTarget as LayerInfo;
            this.md.x = this.mouseX;
            this.md.y = this.mouseY;
            if (this.l.nr != this.doc.anr)
            {
                this.MarkLayer(this.l, true);
            }
            stage.addEventListener(MouseEvent.MOUSE_UP, this.LayerMouseUp, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.LayerMouseMove, false, 1, true);
            return;
        }// end function

        private function LayerMouseMove(event:MouseEvent) : void
        {
            if (!this.draggin)
            {
                this.draggin = true;
                this.marker = new BoxMarker(this.scroller.visible ? (196) : (212));
                this.addChild(this.marker);
                this.ghost = new Bitmap(new BitmapData(this.l.width, this.l.height, true, 16777215));
                this.ghost.bitmapData.draw(this.l);
                this.ghost.alpha = 0.5;
                this.addChild(this.ghost);
                this.ghost.x = 4 + this.l.x;
                this.ghost.y = 44 + this.l.y;
                this.marker.x = 4;
                this.marker.y = this.ghost.y - 1;
                if (!this.doc.transparent && this.l.nr == 0)
                {
                    this.marker.visible = false;
                }
            }
            else
            {
                this.ghost.x = 4 + this.l.x - (this.md.x - this.mouseX);
                this.ghost.y = 44 + this.l.y - (this.md.y - this.mouseY);
                if (this.mouseX < 0 || this.mouseX > 220 || this.mouseY < 0 || this.mouseY > this.bbar.y)
                {
                    if (this.marker.alpha == 1)
                    {
                        this.marker.alpha = 0;
                    }
                }
                else
                {
                    if (this.marker.alpha == 0)
                    {
                        this.marker.alpha = 1;
                    }
                    this.nr = int((this.holder.mouseY + 18) / 39);
                    if (this.nr < 0)
                    {
                        this.nr = 0;
                    }
                    else if (!this.doc.transparent && this.nr > this.holder.numChildren - 2)
                    {
                        this.nr = this.holder.numChildren - 1;
                    }
                    else if (this.nr > (this.holder.numChildren - 1))
                    {
                        this.nr = this.holder.numChildren;
                    }
                    this.marker.y = 44 + this.nr * 39 - 1;
                }
            }
            return;
        }// end function

        private function LayerMouseUp(event:MouseEvent) : void
        {
            var _loc_2:Canvas = null;
            var _loc_3:int = 0;
            var _loc_4:Layer = null;
            if (this.draggin)
            {
                this.draggin = false;
                if (this.trash.hitTestPoint(stage.mouseX, stage.mouseY))
                {
                    ed.cm.DeleteLayer();
                }
                else if (this.newlayer.hitTestPoint(stage.mouseX, stage.mouseY))
                {
                    ed.cm.DuplicateLayer();
                }
                else if (this.marker != null && this.marker.alpha == 1 && this.marker.visible)
                {
                    this.nr = this.holder.numChildren - this.nr;
                    if (this.nr > this.l.nr)
                    {
                        var _loc_5:String = this;
                        var _loc_6:* = this.nr - 1;
                        _loc_5.nr = _loc_6;
                    }
                    if (this.nr != this.l.nr)
                    {
                        ed.cm.OrderLayer(this.nr);
                    }
                }
                this.RemoveMarkers();
                if (!this.hitTestPoint(stage.mouseX, stage.mouseY))
                {
                    _loc_3 = ed.area.numChildren - 1;
                    while (_loc_3 >= 0)
                    {
                        
                        if ((ed.area.getChildAt(_loc_3) as Canvas).masking.hitTestPoint(stage.mouseX, stage.mouseY))
                        {
                            _loc_2 = ed.area.getChildAt(_loc_3) as Canvas;
                            if (_loc_2 != ed.canvas)
                            {
                                _loc_4 = ed.canvas.layer.Clone();
                                ed.ActivateCanvas(_loc_2);
                                ed.cm.PasteLayer(_loc_4);
                            }
                            break;
                        }
                        _loc_3 = _loc_3 - 1;
                    }
                }
            }
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.LayerMouseUp, false);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.LayerMouseMove, false);
            return;
        }// end function

        private function RemoveMarkers() : void
        {
            if (this.marker != null)
            {
                removeChild(this.marker);
                this.marker = null;
            }
            if (this.ghost != null)
            {
                removeChild(this.ghost);
                this.ghost.bitmapData.dispose();
                this.ghost.bitmapData = null;
                this.ghost = null;
            }
            return;
        }// end function

        private function HolderMouseOver(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.Click);
            return;
        }// end function

        private function HolderMouseOut(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function GripMouseOver(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.ResizeHeight);
            return;
        }// end function

        private function GripMouseOut(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function GripMouseMove(event:MouseEvent) : void
        {
            this.Resize(220, this.grip.y + 18);
            this.scroller.SetSize(this.grip.y - 53);
            this.bbar.y = this.grip.y - 8;
            return;
        }// end function

        private function GripMouseDown(event:MouseEvent) : void
        {
            ed.cursor.freeze = true;
            this.grip.startDrag(false, new Rectangle(this.grip.x, 103, 0, 600));
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.GripMouseMove, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.GripMouseUp, false, 0, true);
            return;
        }// end function

        private function GripMouseUp(event:MouseEvent) : void
        {
            this.grip.stopDrag();
            ed.cursor.freeze = false;
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.GripMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.GripMouseUp, false);
            this.GripMouseMove(null);
            if (event.target != this.grip)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
